package com.example.materialcostsystembackend.controller;

import com.example.materialcostsystembackend.model.Part;
import com.example.materialcostsystembackend.repository.PartRepository;
import com.example.materialcostsystembackend.service.PartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/parts")
    //@CrossOrigin(origins = "*")
public class PartController {
    @Autowired
    private PartService service;

    // 点击前端 “确定” 按钮时调用
    @PostMapping("/confirm")
    public ResponseEntity<String> confirm(@RequestParam String name,
                                          @RequestParam String specification,
                                          @RequestParam String unit,
                                          @RequestParam Double price,
                                          @RequestParam(defaultValue = "1") int qty) {
        service.addPart(name, specification, unit, price, qty);
        return ResponseEntity.ok("零件数量已更新");
    }

    // 前端全局统计时获取数据

    @GetMapping("/statistics")
    public ResponseEntity<List<Part>> getAllParts() {
        List<Part> parts = service.getAllParts();  // ✅ 调 service
        return ResponseEntity.ok(parts);
    }
}
